import urllib2
import xml.etree.cElementTree as ET

class Arsenal:    
    def __init__(self, realm):
        self._realm = realm
    
    def _read_arsenal(self, url):
        req = urllib2.Request(url)
        req.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1")
        res = urllib2.urlopen(req)
        tree = ET.parse(res)
        # ET.dump(tree)
        # root = tree.getroot()
        # xmldict = XmlDictConfig(root)
        return tree #simplejson.dumps(xmldict)
    
    def get_char_data(self, char_name):
        return self._read_arsenal("http://eu.wowarmory.com/character-sheet.xml?r=%s&n=%s" % (self._realm, char_name))
    
    def get_char_reputation(self, char_name):
        return self._read_arsenal("http://eu.wowarmory.com/character-reputation.xml?r=%s&n=%s" % (self._realm, char_name))
    
    def get_char_skills(self, char_name):
        return self._read_arsenal("http://eu.wowarmory.com/character-skills.xml?r=%s&n=%s" % (self._realm, char_name))
    
    def get_char_talents(self, char_name):
        return self._read_arsenal("http://eu.wowarmory.com/character-talents.xml?r=%s&n=%s" % (self._realm, char_name))
    
    def get_guild_data(self, guild_name):
        from urllib import quote_plus
        # encode guild name (e.g.: "Armee des Lichtes" --> "Armee+des+Lichtes")
        encoded_guild_name = quote_plus(guild_name)
        return self._read_arsenal("http://eu.wowarmory.com/guild-info.xml?r=%s&n=%s" % (self._realm, encoded_guild_name))
    